package com.hceax.servlet;

import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.Enumeration;  
  
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
/** 
 * 获取Http协议的请求行，请求头 
 * @author Starjuly 
 * 
 */  
@WebServlet(name = "header",urlPatterns = {"/head.do"})
public class head extends HttpServlet {
  
       
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        System.out.println("请求行的方法:" + request.getMethod());  
        System.out.println("请求行的URI:" + request.getRequestURI());  
        System.out.println("请求行的URL:" + request.getRequestURL());  
        System.out.println("请求行的版本:" + request.getProtocol());  
          
        System.out.println("请求头:"+request.getHeader("User-Agent"));  
        //利用迭代器遍历所有的请求头  
        Enumeration names = request.getHeaderNames();   
        while(names.hasMoreElements()){  
            //获取请求头  
             String  nextElement = (String)names.nextElement();  
             //获取每个请求头的内容  
             String header = request.getHeader(nextElement);   
             System.out.print(nextElement+" : ");  
             System.out.println(header);  
        }  
          
    }  
  
       
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
  
        doGet(request,response);  
    }  
  
}  
